﻿using Framework.Common;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Framework.UI
{
    public class UIManager : IUIManager, IModule
    {
        private Dictionary<string, UIBase> m_UIDics;
        private Stack<UIBase> m_UIStack;

        #region 模块接口

        public int Priority
        {
            get
            {
                return ModulePriority.UI;
            }
        }
        public void Init()
        {
            m_UIDics = new Dictionary<string, UIBase>();
            m_UIStack = new Stack<UIBase>();
        }
        public void Reset()
        {
            if(m_UIDics != null)
            {
                foreach (var ui in m_UIDics)
                    ui.Value.OnRelease();
            }
            m_UIDics.Clear();
            if (m_UIStack != null)
                m_UIStack.Clear();
        }
        public void Update(float frameTime, float realTime)
        {
            if (m_UIDics == null) return;
            foreach (var ui in m_UIDics)
            {
                ui.Value.OnUpdate(frameTime, realTime);
            }
        }
        public void Destroy()
        {
            Reset();
            m_UIStack = null;
            m_UIDics = null;
        }

        #endregion

        public void ShowScene(string uiName, object userData)
        {
            if (m_UIDics == null) return;
            UIScene uiScene = null;
            if(m_UIDics.ContainsKey(uiName))
                uiScene = m_UIDics[uiName] as UIScene;
            if(uiScene == null)
            {
                GameObject uiGo = CreateUIGameObject(uiName);
                if (uiGo == null)
                    throw new FrameworkException(string.Format("Create {0} Error", uiName));
                uiScene = uiGo.GetComponent<UIScene>();
                if (uiScene == null)
                    throw new FrameworkException(string.Format("Get {0} UIScene Error", uiName));
                uiScene.Init(UIType.SCENE, uiName);
                m_UIDics.Add(uiName, uiScene);
                m_UIStack.Push(uiScene);
            }
            uiScene.OnShow(userData);
        }
        public void ShowPanel(string uiName, object userData)
        {
            if (m_UIDics == null) return;
            UIPanel uiPanel = null;
            if (m_UIDics.ContainsKey(uiName))
                uiPanel = m_UIDics[uiName] as UIPanel;
            if (uiPanel == null)
            {
                GameObject uiGo = CreateUIGameObject(uiName);
                if (uiGo == null)
                    throw new FrameworkException(string.Format("Create {0} Error", uiName));
                uiPanel = uiGo.GetComponent<UIPanel>();
                if (uiPanel == null)
                    throw new FrameworkException(string.Format("Get {0} UIPanel Error", uiName));
                uiPanel.Init(UIType.PANEL, uiName);
                m_UIDics.Add(uiName, uiPanel);
                m_UIStack.Push(uiPanel);
            }
            uiPanel.OnShow(userData);
        }
        public void ShowTips(string uiName, object userData)
        {
            if (m_UIDics == null) return;
            UITips uiTips = null;
            if (m_UIDics.ContainsKey(uiName))
                uiTips = m_UIDics[uiName] as UITips;
            if (uiTips == null)
            {
                GameObject uiGo = CreateUIGameObject(uiName);
                if (uiGo == null)
                    throw new FrameworkException(string.Format("Create {0} Error", uiName));
                uiTips = uiGo.GetComponent<UITips>();
                if (uiTips == null)
                    throw new FrameworkException(string.Format("Get {0} UITips Error", uiName));
                uiTips.Init(UIType.TIPS, uiName);
                m_UIDics.Add(uiName, uiTips);
            }
            uiTips.OnShow(userData);
        }
        public void CreateUI(string uiName, string extension, object userData)
        {
            if (m_UIDics == null) return;
            string name = string.Format("{0}{1}", uiName, extension);
            UIWidget uiWidget = null;
            if (m_UIDics.ContainsKey(name))
                uiWidget = m_UIDics[name] as UIWidget;
            if (uiWidget == null)
            {
                GameObject uiGo = CreateUIGameObject(name);
                if (uiGo == null)
                    throw new FrameworkException(string.Format("Create {0} Error", name));
                uiWidget = uiGo.GetComponent<UIWidget>();
                if (uiWidget == null)
                    throw new FrameworkException(string.Format("Get {0} UIWidget Error", name));
                uiWidget.Init(UIType.WIDGET, name);
                m_UIDics.Add(name, uiWidget);
            }
            uiWidget.OnShow(userData);
        }
        public void DestroyUI(string uiName)
        {
            if (m_UIDics == null) return;
            if (!m_UIDics.ContainsKey(uiName)) return;
            UIBase uiBase = m_UIDics[uiName];
            switch (uiBase.UIType)
            {
                case UIType.SCENE:
                case UIType.PANEL:
                    if(m_UIStack.Peek().UIName.Equals(uiName))
                    {
                        m_UIStack.Pop();
                        UIBase uiTop = m_UIStack.Peek();
                        if (uiTop != null)
                            uiTop.OnActivate();
                    }
                    break;
            }
            m_UIDics.Remove(uiName);
            uiBase.OnRelease();
            DestroyUIGameObject(uiName);
        }
        private GameObject CreateUIGameObject(string uiName)
        {
            return null;
        }
        private void DestroyUIGameObject(string uiName)
        {

        }
    }
    public enum UIType
    {
        SCENE,
        PANEL,
        TIPS,
        WIDGET
    }
}
